$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/busercommodityinfo/list',
        datatype: "json",
        colModel: [			
			{ label: '商品名称', name: 'id', index: 'id',align: "center", width: 50, key: true ,formatter:function(value,options,rowData){
					var name=rowData["commodityName"];
					return "<a  onclick='update("+value+")'>"+name+"</a>";
				}
			},
			// { label: '商品名称', name: 'commodityName', index: 'commodity_name',align: "center", width: 80 },
			{ label: '用户ID', name: 'userId', index: 'user_id',align: "center", width: 80 },
			{ label: '商品图片', name: 'commodityImgUrl', index: 'commodity_img_url',align: "center", width: 80 , sortable: false, editable: false, formatter: alarmFormatter},
			{ label: '商品描述', name: 'commodityDesc', index: 'commodity_desc',align: "center", width: 80 ,formatter:function(value,options,rowData){
				if(value!=null && value.length>=12) {
					var content = value.substring(0, 12);
					return "<a title='"+value+"' style='color: #333'>"+content+"</a>";
				}else {
					return "<a title='" + value + "'style='color: #333'>" + value + "</a>";
				}
				}
			},
			{ label: '商品状态', name: 'commodityStatus', index: 'commodity_status',align: "center", width: 80,formatter:function(value,options,rowData){
					if( value===0 ){
						return '已删除';
					}else{
						return '正常';
					}
				}
			},
			{ label: '商品价格', name: 'commodityPrice', index: 'commodity_price',align: "center", width: 80 }, 			
			{ label: '商品原价', name: 'originalCommodityPrice', index: 'original_commodity_Price',align: "center", width: 80 }, 			
			{ label: '创建时间', name: 'createTime', index: 'create_time',align: "center", width: 80 }, 			
			{ label: '修改时间', name: 'modifyTime', index: 'modify_time',align: "center", width: 80 }, 			
        ],
		viewrecords: true,
        height: '100%',
        rowNum: 8,
		rowList : [8,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
		postData:{"commodityName":vm.q.commodityName,"userId":vm.q.userId},
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });

        	$("#jqGrid").closest("ui-jqgrid tr.jqgrow td") .css({"text-overflow":"ellipsis"});
        }
    });
});
function alarmFormatter(cellvalue, options, rowdata) {
	if(cellvalue==null || cellvalue==""){
		return  "<img src='" +'https://lktvtool.oss-cn-shenzhen.aliyuncs.com/icon/236A5FF0-52DE-4644-B66C-E9555C97693D.png'+ "' width='60' height='60'/>";
	}else {
		return "<img src='" + cellvalue + "' width='60' height='60'/>";
	}
}
var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		bUserCommodityInfo: {},
		q:{
			commodityName:"",
			userId:""
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.bUserCommodityInfo = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			if(vm.bUserCommodityInfo.commodityName==null||vm.bUserCommodityInfo.commodityName==""){
				alert("请输入完整信息");
				return;
			}
			var url = vm.bUserCommodityInfo.id == null ? "sys/busercommodityinfo/save" : "sys/busercommodityinfo/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.bUserCommodityInfo),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/busercommodityinfo/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/busercommodityinfo/info/"+id, function(r){
                vm.bUserCommodityInfo = r.bUserCommodityInfo;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
				postData:{"commodityName":vm.q.commodityName,"userId":vm.q.userId},
            }).trigger("reloadGrid");
		}
	}
});
function update(e) {
	vm.showList = false;
	vm.title = "修改";
	vm.getInfo(e)
}